<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hadd(1,'0')">添加权限</el-button>
        </div>
        <el-table border row-key="id" :data="list">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="hadd(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="heait(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <div>
        <el-dialog
          :visible.sync="showDialog"
          :title="isEait ? '编辑' :'添加'"
          @close="hclose"
        >
          <!-- 表单内容 -->
          <el-form ref="form" :rules="rules" :model="formData" label-width="100px">
            <el-form-item label="权限名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识" prop="code">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
            <el-form-item>
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="hsumite">确定</el-button>
            </el-form-item>
          </el-form>

        </el-dialog></div>
    </div>
  </div>
</template>
<script>
import { toTree } from '@/utils/tranListToTreeData.js'
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permisson.js'
export default {
  data() {
    const validName = (rule, value, callback) => {
      let feichongname = this.feichonglist.filter(item => item.pid === this.formData.pid)
      if (this.isEait) {
        feichongname = this.feichonglist.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id)
      }
      console.log(feichongname)
      feichongname.map(item => item.name).includes(value)
        ? callback(new Error(value + '已经占用'))
        : callback()
    }
    const validCode = (rule, value, callback) => {
      let feichongCode = this.feichonglist.filter(item => item.pid === this.formData.pid)
      if (this.isEait) {
        feichongCode = this.feichonglist.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id)
      }
      feichongCode.map(item => item.code).includes(value)
        ? callback(new Error(value + '已经占用'))
        : callback()
    }
    return {
      isEait: false,
      showDialog: false, // 是否显示弹层
      list: [],
      feichonglist: [],
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [{ message: '不能为空', required: true, trigger: 'blur' }, { validator: validName, trigger: 'blur' }],
        code: [{ message: '不能为空', required: true, trigger: 'blur' }, { validator: validCode, trigger: 'blur' }]
      }

    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 获取权限列表信息
    async  loadPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.feichonglist = res.data.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
      this.list = toTree(res.data)
    },
    // 点击添加
    hadd(type, pid) {
      this.isEait = false
      this.showDialog = true
      // 记录当前添加的关键信息
      this.formData.type = type
      this.formData.pid = pid
    },
    hsumite() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.isEait) {
            this.doeait()
          } else {
            await addPermission(this.formData)
            this.showDialog = false
            this.loadPermissionList()
          }
        }
      })
    },
    hclose() {
      // 1. 重置表单
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      // . 清空校验
      this.$refs.form.resetFields()
    },
    // 点击删除
    hDel(id) {
      this.$confirm('你确定要删除吗？').then(async() => {
        await delPermission(id)
        this.$message.success('删除成功')
        this.loadPermissionList()
      }).catch((err) => {
        console.log(err)
      })
    },
    // 点击编辑更新
    async  heait(id) {
      this.isEait = true
      this.showDialog = true
      const res = await getPermissionDetail(id)
      this.formData = res.data
    },
    async doeait() {
      await updatePermission(this.formData)
      this.$message.success('修改成功')
      this.showDialog = false
      this.loadPermissionList()
    }
  }
}
</script>
